package com.company.lianbiao;

public class LinkedList01 {

    private Node node;
    private Node first;

    /**
     * 判断链表是否为空
     */
    public boolean isEmpty(){
        return this.node == null;
    }

    /**
     * 链表插入
     * @param data
     */
    public void continuensert(String data){
        Node newNode = new Node(data);

        if(isEmpty()){
            first = newNode;
            this.node = newNode;
        }else{
            this.node.next = newNode;
            this.node = newNode;
        }
    }

    /**
     * 链表的反转
     */
    public void reversal(){
        Node printNode = first;

        while(printNode != null){

        }

    }

    /**
     * 链表输出
     */
    public void print(){
        Node printNode = first;
        while(printNode != null){
            System.out.println(printNode.data);
            printNode = printNode.next;
        }
    }

    /**
     * 链表查找
     */
    public Node search(String data){
        Node printNode = first;
        while(printNode != null){
            if(data.equals(printNode.data)){
                break;
            }
            printNode = printNode.next;
        }
        return printNode;
    }

    /**
     * 链表的删除
     */
    public void delete(String data){
        Node printNode = first;
        Node beforeNode = null;
        //如果是第一个节点
        if (printNode.data.equals(data)){
            first = printNode.next;
            return;
        }

        //不是第一个节点删除
        while (printNode != null){
            //如果命中了,那么
            if(printNode.data.equals(data)){
                beforeNode.next = printNode.next;
            }

            beforeNode = printNode;
            printNode = printNode.next;
        }
    }

}
